home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Utils / Software6 / Product13 / googlebar-0.9.5.06-fx.xpi / chrome / googlebar.jar / content / googlebarHighlight.js < prev    next >
Text File  |  2005-02-21  |  13KB  |  433 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Googlebar for Mozilla.
  15.  *
  16.  * The Initial Developer of the Original Code is Andy Edmonds.
  17.  * Portions created by the Initial Developer are Copyright (C) 2001
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Andy Boughton
  22.  *  Andrew Houghton
  23.  *  John Woods
  24.  *  Bernd Kuemmerlen
  25.  *  Robert Mulcahy
  26.  *  Alfred Kayser
  27.  *  Reflex
  28.  *  Gary Turnbull
  29.  *  Raj Bhaskar
  30.  *  Joachim Thewes
  31.  *  Henrik Gemal
  32.  *  Robert Fernandes
  33.  *  Joe Chellman
  34.  *  Franki Cheung
  35.  *  Alban Fonrouge
  36.  *  Martin Hassman
  37.  *  Ufuk Kayserilioglu
  38.  *  Yoni Gilad
  39.  *  Tim Schmidt
  40.  *  timeless
  41.  *  Francis Turner
  42.  *
  43.  * Alternatively, the contents of this file may be used under the terms of
  44.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  45.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  46.  * in which case the provisions of the GPL or the LGPL are applicable instead
  47.  * of those above. If you wish to allow use of your version of this file only
  48.  * under the terms of either the GPL or the LGPL, and not to allow others to
  49.  * use your version of this file under the terms of the MPL, indicate your
  50.  * decision by deleting the provisions above and replace them with the notice
  51.  * and other provisions required by the GPL or the LGPL. If you do not delete
  52.  * the provisions above, a recipient may use your version of this file under
  53.  * the terms of any one of the MPL, the GPL or the LGPL.
  54.  *
  55.  * ***** END LICENSE BLOCK ***** */
  56.  
  57. const GB_PREF_TERM_COLORS = "termColors";
  58. const GB_PREF_UPD_COLORS  = "updateColors";
  59. const GB_HIGHLIGHT_ID = "googlebar_highlight";
  60.  
  61. var myGooglebarHighlight = {
  62.  
  63.     /*** Variable Declarations ***/
  64.  
  65.     mDelayHighlight: false,
  66.  
  67.     // DOMRange used during highlighting
  68.     mSearchRange: null,
  69.     mStartPt: null,
  70.     mEndPt: null,
  71.  
  72.     // Is the highlight active ?
  73.     mHighlightOn: false,
  74.  
  75.     // What was the term used for the highlighting ?
  76.     mHighlightString: "",
  77.  
  78.     // Save selection during highlighting
  79.     mSelection: null,
  80.     mSaveSelection: null,
  81.  
  82.  
  83.     // Colors for highlighting
  84.     mTermColors: null,
  85.  
  86.     /*** Function Declarations ***/
  87.  
  88.     checkHighlight: function(aCritValue) {
  89.         if (this.mDelayHighlight) {
  90.             this.mDelayHighlight = false;
  91.         }
  92.         if (!aCritValue) {
  93.             aCritValue = myGooglebarUtil.getElement('googlebarCriteria').value;
  94.         }
  95.  
  96.         var myTerms = googlebarFindTerms(aCritValue);
  97.         var myHighlightButton = myGooglebarUtil.getElement("highlight-button");
  98.  
  99.         if (myTerms.length > 0) {
  100.             if (this.mHighlightOn) {
  101.                 this.highLightInPage("on");
  102.             }
  103.             myHighlightButton.setAttribute("disabled", false);
  104.             myHighlightButton.setAttribute("activated", false);
  105.         }
  106.         else {
  107.             if (this.mHighlightOn) {
  108.                 this.highLightInPage("off");
  109.                 this.mHighlightOn = false;
  110.             }
  111.             myHighlightButton.setAttribute("disabled", true);
  112.             myHighlightButton.setAttribute("activated", true);
  113.         }
  114.     },
  115.  
  116.     delayHighlight: function() {
  117.         if (this.mDelayHighlight) {
  118.             return;
  119.         }
  120.         else {
  121.             this.mDelayHighlight = true;
  122.             setTimeout("myGooglebarHighlight.checkHighlight()", 1000);
  123.         }
  124.     },
  125.  
  126.     displayCheck: function() {
  127.         if (this.mHighlightOn) {
  128.             setTimeout("myGooglebarHighlight.refreshHighlight()", 100);
  129.         }
  130.     },
  131.  
  132.     getTermColors: function() {
  133.         return this.mTermColors;
  134.     },
  135.  
  136.     highlight: function(aRange, aNode) {
  137.         var startContainer = aRange.startContainer;
  138.         var startOffset = aRange.startOffset;
  139.         var endOffset = aRange.endOffset;
  140.         var docfrag = aRange.extractContents();
  141.         var before = startContainer.splitText(startOffset);
  142.         var parent = before.parentNode;
  143.         aNode.appendChild(docfrag);
  144.         parent.insertBefore(aNode, before);
  145.         return aNode;
  146.     },
  147.  
  148.     highlightDoc: function(aWord, aColor, aWindow) {
  149.         if (!aWindow) {
  150.             aWindow = window.self;
  151.         }
  152.  
  153.         for (var i = 0; aWindow.frames && i < aWindow.frames.length; i++) {
  154.             this.highlightDoc(aWord, aColor, aWindow.frames[i]);
  155.         }
  156.  
  157.         var doc = aWindow.document;
  158.         if (!doc) {
  159.             return;
  160.         }
  161.  
  162.         // Remove highligthing
  163.         if (!aColor) {
  164.             this.removeHighlight(aWindow);
  165.             return;
  166.         }
  167.  
  168.         if (!("body" in doc)) {
  169.             return;
  170.         }
  171.         
  172.         var body = doc.body;
  173.         var count = body.childNodes.length;
  174.         this.mSearchRange = doc.createRange();
  175.         this.mStartPt = doc.createRange();
  176.         this.mEndPt = doc.createRange();
  177.  
  178.         var baseNode = doc.createElement("layer");
  179.         baseNode.setAttribute("style", "-moz-user-select: -moz-all; background-color: " + aColor + ";");
  180.         baseNode.setAttribute("id", GB_HIGHLIGHT_ID);
  181.  
  182.         this.mSearchRange.setStart(body, 0);
  183.         this.mSearchRange.setEnd(body, count);
  184.  
  185.         this.mStartPt.setStart(body, 0);
  186.         this.mStartPt.setEnd(body, 0);
  187.         this.mEndPt.setStart(body, count);
  188.         this.mEndPt.setEnd(body, count);
  189.         this.highlightText(aWord, baseNode);
  190.     },
  191.  
  192.     highLightInPage: function(aOverride) {
  193.         try {
  194.             if (GB_PREF_OBJ.getBoolPref(GB_PREF_UPD_COLORS)) {
  195.                 var tempColor = GB_PREF_OBJ.getCharPref(GB_PREF_TERM_COLORS).split(',');
  196.                 if (this.mTermColors.length == tempColor.length) {
  197.                     this.mTermColors = tempColor;
  198.                 }
  199.                 else {
  200.                     myGooglebarUtil.logMessage("Problem retrieving updated colors - wrong number of colors");
  201.                 }
  202.             }
  203.         }
  204.         catch (e) {
  205.             myGooglebarUtil.logMessage("Problem retrieving updated colors - pref error");
  206.         }
  207.  
  208.         // Initialize to current document
  209.         var gTheDocument = window._content.document;
  210.         var critBox = myGooglebarUtil.getElement("googlebarCriteria");
  211.         var hiBtn = myGooglebarUtil.getElement("highlight-button");
  212.         var term = critBox.value;
  213.  
  214.         this.removeHighlight();
  215.  
  216.         if (this.mHighlightOn) {
  217.             this.mHighlightOn = false;
  218.             // this.highlightDoc(null, null);
  219.             googlebarCheckSearchbuttons();
  220.  
  221.             // Exit if the term had not changed
  222.             //  => means that the user clicked to remove highlighting
  223.             if (this.mHighlightString == term  && aOverride != "on") {
  224.                 hiBtn.setAttribute("activated", "false");
  225.                 return;
  226.             }
  227.         }
  228.  
  229.         this.mHighlightOn = true;
  230.         this.mHighlightString = term;
  231.         hiBtn.setAttribute("activated", "true");
  232.  
  233.         // wanted to use selection to extend to the word
  234.         // and compare with the found range, in order to match
  235.         // only whole words
  236.  
  237.         /*
  238.             this.mSelection = window._content.getSelection();
  239.             this.mSaveSelection = new Array();
  240.             for (var i = 0; i < this.mSelection.rangeCount; i++) {
  241.                 this.mSaveSelection.push(this.mSelection.getRangeAt(i));
  242.             }
  243.             this.mSelection.removeAllRanges();
  244.         */
  245.  
  246.         var myTerms = googlebarFindTerms(term);
  247.         for (var i = 0; i < myTerms.length; i++) {
  248.             // only get terms at least 2 characters long
  249.             if (myTerms[i].length > 1) {
  250.                 this.highlightDoc(myTerms[i], this.mTermColors[i % 10]);
  251.             }
  252.         }
  253.  
  254.         /*
  255.             // Revert to previous selection
  256.             this.mSelection.removeAllRanges();
  257.             for (var i = 0; i < this.mSaveSelection.length; i++) {
  258.                 this.mSelection.addRange(this.mSaveSelection[i]);
  259.             }
  260.         */
  261.  
  262.         googlebarCheckSearchbuttons();
  263.     },
  264.  
  265.     highlightText: function(aWord, aBaseNode) {
  266.         var retRange = null;
  267.         while((retRange = GB_FIND_OBJ.Find(aWord, this.mSearchRange, this.mStartPt, this.mEndPt))) {
  268.             // check that we have a whole word
  269.             //this.mSelection.addRange(range);
  270.  
  271.             // Highlight
  272.             var nodeSurround = aBaseNode.cloneNode(true);
  273.             var node = this.highlight(retRange, nodeSurround);
  274.             this.mStartPt = node.ownerDocument.createRange();
  275.             this.mStartPt.setStart(node, node.childNodes.length);
  276.             this.mStartPt.setEnd(node, node.childNodes.length);
  277.         }
  278.     },
  279.  
  280.     initialize: function() {
  281.         this.readPrefs();
  282.     },
  283.  
  284.     isHighlightOn: function() {
  285.         return this.mHighlightOn;
  286.     },
  287.  
  288.     readPrefs: function() {
  289.         try {
  290.             this.mTermColors = GB_PREF_OBJ.getCharPref(GB_PREF_TERM_COLORS).split(',');
  291.         }
  292.         catch(e) { myGooglebarUtil.logMessage(  "\n" + e + "\n" );
  293.             myGooglebarUtil.logMessage('Resetting ' + GB_PREF_TERM_COLORS + ' pref');
  294.             GB_PREF_OBJ.setCharPref(GB_PREF_TERM_COLORS, "yellow,lightpink,aquamarine,darkgoldenrod,darkseagreen,lightgreen,rosybrown,seagreen,chocolate,violet");
  295.         }
  296.  
  297.         this.mTermColors = GB_PREF_OBJ.getCharPref(GB_PREF_TERM_COLORS).split(',');
  298.         GB_PREF_OBJ.setBoolPref(GB_PREF_UPD_COLORS, false);
  299.     },
  300.  
  301.     refreshHighlight: function() {
  302.         var hiBtn = myGooglebarUtil.getElement("highlight-button");
  303.         hiBtn.click();
  304.         hiBtn.click();
  305.     },
  306.     
  307.     removeHighlight: function(aWindow) {
  308.         if (!aWindow) {
  309.             aWindow = window.self;
  310.         }
  311.  
  312.         var i = 0;
  313.  
  314.         for (i = 0; aWindow.frames && i < aWindow.frames.length; i++) {
  315.             this.removeHighlight(aWindow.frames[i]);
  316.         }
  317.  
  318.         var doc = aWindow.document;
  319.         if(!doc) { return; }
  320.         var body = doc.body;
  321.         if(!body) { return; }
  322.  
  323.         var elem = null;
  324.         var child = null;
  325.            var docfrag = null;
  326.         var next = null;
  327.         var parent = null;
  328.         
  329.         while ((elem = doc.getElementById(GB_HIGHLIGHT_ID))) {
  330.             child = null;
  331.             docfrag = doc.createDocumentFragment();
  332.             next = elem.nextSibling;
  333.             parent = elem.parentNode;
  334.             while((child = elem.firstChild)) {
  335.                 docfrag.appendChild(child);
  336.             }
  337.             parent.removeChild(elem);
  338.             parent.insertBefore(docfrag, next);
  339.         }
  340.  
  341.         var count = body.childNodes.length;
  342.         var terms = myGooglebarUtil.getElement("googlebarCriteria").value;
  343.         var termsArray = googlebarFindTerms(terms);
  344.  
  345.         for (i = 0; i < termsArray.length; i++) {
  346.             var term = termsArray[i];
  347.  
  348.             var searchRange = doc.createRange();
  349.             searchRange.setStart(body, 0);
  350.             searchRange.setEnd(body, count);
  351.  
  352.             var startPt = doc.createRange();
  353.             startPt.setStart(body, 0);
  354.             startPt.setEnd(body, 0);
  355.  
  356.             var endPt = doc.createRange();
  357.             endPt.setStart(body, count);
  358.             endPt.setEnd(body, count);
  359.  
  360.             var retRange = null;
  361.  
  362.             while((retRange = GB_FIND_OBJ.Find(term, searchRange, startPt, endPt))) {
  363.                 var startContainer = retRange.startContainer;
  364.                 elem = null;
  365.  
  366.                 try {
  367.                     elem = startContainer.parentNode;
  368.                 }
  369.                 catch (e) {}
  370.                 
  371.                 if (elem) {
  372.                     child = null;
  373.                     docfrag = null;
  374.                     next = null;
  375.                     parent = null;
  376.  
  377.                     if (elem.getAttribute("id") == GB_HIGHLIGHT_ID) {
  378.                         docfrag = doc.createDocumentFragment();
  379.                         next = elem.nextSibling;
  380.                         parent = elem.parentNode;
  381.  
  382.                         while((child = elem.firstChild)) {
  383.                             docfrag.appendChild(child);
  384.                         }
  385.  
  386.                         startPt = doc.createRange();
  387.                         startPt.setStartAfter(elem);
  388.  
  389.                         parent.removeChild(elem);
  390.                         parent.insertBefore(docfrag, next);
  391.                     }
  392.                     else {
  393.                         try {
  394.                             while((elem = elem.parentNode)) {
  395.                                 if (elem.getAttribute("id") == GB_HIGHLIGHT_ID) {
  396.                                     docfrag = doc.createDocumentFragment();
  397.                                     next = elem.nextSibling;
  398.                                     parent = elem.parentNode;
  399.  
  400.                                     while((child = elem.firstChild)) {
  401.                                         docfrag.appendChild(child);
  402.                                     }
  403.  
  404.                                     docfrag.appendChild(elem.cloneNode(true));
  405.  
  406.                                     startPt = doc.createRange();
  407.                                     startPt.setStartAfter(elem);
  408.  
  409.                                     parent.removeChild(elem);
  410.                                     parent.insertBefore(docfrag, next);
  411.                                     break;
  412.                                 }
  413.                             }
  414.                         }
  415.                         catch(e) {}
  416.                         
  417.                         startPt = doc.createRange();
  418.                         startPt.setStart(retRange.endContainer, retRange.endOffset);
  419.                     }
  420.                 }
  421.                 else {
  422.                     startPt = doc.createRange();
  423.                     startPt.setStart(retRange.endContainer, retRange.endOffset);
  424.                 }
  425.  
  426.                 startPt.collapse(true);
  427.             }
  428.         }
  429.     }
  430. };
  431.  
  432. myGooglebarHighlight.initialize();
  433.